import React, {Component} from 'react';
import {Breadcrumb, message, Form, Input, Button, Select, Modal, Table, Space} from "antd";
import {NavLink} from "react-router-dom";
import {connect} from "react-redux";

const {Option} = Select;

@connect(state => ({userInfo: state.userInfo}))
class Content extends Component {
    state = {
        dialog: false,
        loading:false,
        pagelistquery: {
            user: "",
            admin: "",
            state: "0",
            search: "",
            type: "help",
            total: 0,
            page: 1,
            pagesize: 10
        },
        type: "help",
        tableData: [],
        content: {}
    }
    getContentList = async (values) => {
        this.setState({loading:true});
        values = {...this.state.pagelistquery,...values};
        this.setState({type:values.type,pagelistquery:values});
        let result = await this.$axios.post("/admin/contentexamine", values);
        if (result.data.state.type === "SUCCESS") {
            let pagelistquery = this.state.pagelistquery;
            pagelistquery.total = result.data.count;
            this.setState({tableData:result.data.data,pagelistquery: pagelistquery});
            this.setState({loading:false});
        }
    }
    changeState = async (state) => {
        let id;
        if (this.state.pagelistquery.type === "help") {
            id = this.state.content.help_id;
        }
        if (this.state.pagelistquery.type === "activity") {
            id = this.state.content.activity_id;
        }
        if (this.state.pagelistquery.type === "job") {
            id = this.state.content.job_id;
        }
        if (this.state.pagelistquery.type === "oldstuff") {
            id = this.state.content.oldstuff_id;
        }
        if (this.state.pagelistquery.type === "article") {
            id = this.state.content.article_id;
        }
        let data = {
            id: id,
            type: this.state.pagelistquery.type,
            state: state
        };
        let result = await this.$axios.post("/admin/changecontentstate", data);
        if (result.data.state.type === "SUCCESS") {
            message.success("操作成功");
            this.setState({dialog:false})
            this.getContentList();
        }
    }
    shenhe = (row) => {
        this.setState({content: row, dialog: true});
    }
    del = async (id) => {
        let data = {
            id: id,
            type: this.state.pagelistquery.type
        };
        let result = await this.$axios.post("/admin/admindelete", data);
        if (result.data.state.type === "SUCCESS") {
            message.success("删除成功");
            this.getContentList();
        }
    }
    handleSizeChange = (current, size) => {
        let pagelistquery = this.state.pagelistquery
        pagelistquery.pagesize = size;
        this.setState({pagelistquery: pagelistquery});
        this.getContentList();
    }
    handleCurrentChange = (page, pagesize) => {
        let pagelistquery = this.state.pagelistquery;
        pagelistquery.page = page;
        pagelistquery.pagesize = pagesize;
        this.setState({pagelistquery: pagelistquery});
        this.getContentList();
    }
    componentDidMount() {
        this.getContentList();
    }

    render() {
        let {tableData, pagelistquery, type, content, dialog, loading} = this.state;
        tableData.map((item,index)=>{
            return item.key = index;
        })
        const helpColumns = [
            {
                title: "创建日期",
                dataIndex: "createtime",
                render:text => <span>{this.dateFormat(text)}</span>
            },
            {
                title: "作者",
                dataIndex: "nickname",
            },
            {
                title: "标题",
                dataIndex: "help_title",
            },
            {
                title: "分类",
                dataIndex: "help_lable",
            },
            {
                title: "状态",
                dataIndex: "ispublic",
                render: text => {
                    if(text === 1) return <span style={{color: "rgb(108, 187, 122)"}}>审核通过</span>
                    else if (text === 0) return <span style={{color: "#409EFF"}}>未审核</span>
                    else return <span style={{color: "rgb(246, 12, 108)"}}>审核未通过</span>
                }
            },
            {
                title: "审核人",
                dataIndex: "admin",
            },
            {
                title: "操作",
                dataIndex: "help_id",
                render: (text,record) => (
                    <Space size="small">
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.shenhe(record)}}>审核</Button>
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.del(text)}}
                                disabled={this.props.userInfo.uinfo.jurisdiction.issh !== '1' && this.props.userInfo.uinfo.username !== 'admin'}
                        >删除</Button>
                    </Space>
                )
            }
        ];
        const activityColumns = [
            {
                title: "创建日期",
                dataIndex: "createtime",
                render:text => <span>{this.dateFormat(text)}</span>
            },
            {
                title: "作者",
                dataIndex: "nickname",
            },
            {
                title: "活动名称",
                dataIndex: "activity_title",
            },
            {
                title: "地点",
                dataIndex: "activity_locale",
            },
            {
                title: "分类",
                dataIndex: "activity_lable",
            },
            {
                title: "状态",
                dataIndex: "ispublic",
                render: text => {
                    if(text === 1) return <span style={{color: "rgb(108, 187, 122)"}}>审核通过</span>
                    else if (text === 0) return <span style={{color: "#409EFF"}}>未审核</span>
                    else return <span style={{color: "rgb(246, 12, 108)"}}>审核未通过</span>
                }
            },
            {
                title: "审核人",
                dataIndex: "admin",
            },
            {
                title: "操作",
                dataIndex: "activity_id",
                render: (text,record) => (
                    <Space size="small">
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.shenhe(record)}}>审核</Button>
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.del(text)}}
                                disabled={this.props.userInfo.uinfo.jurisdiction.issh !== '1' && this.props.userInfo.uinfo.username !== 'admin'}
                        >删除</Button>
                    </Space>
                )
            }
        ];
        const oldstuffColumns = [
            {
                title: "创建日期",
                dataIndex: "createtime",
                render:text => <span>{this.dateFormat(text)}</span>
            },
            {
                title: "作者",
                dataIndex: "nickname",
            },
            {
                title: "图片",
                dataIndex: "oldstuff_img",
                render: text => <img src={text} alt="图片地址错误" height="100px"/>
            },
            {
                title: "名称",
                dataIndex: "oldstuff_name",
            },
            {
                title: "分类",
                dataIndex: "oldstuff_lable",
            },
            {
                title: "状态",
                dataIndex: "ispublic",
                render: text => {
                    if(text === 1) return <span style={{color: "rgb(108, 187, 122)"}}>审核通过</span>
                    else if (text === 0) return <span style={{color: "#409EFF"}}>未审核</span>
                    else return <span style={{color: "rgb(246, 12, 108)"}}>审核未通过</span>
                }
            },
            {
                title: "审核人",
                dataIndex: "admin",
            },
            {
                title: "操作",
                dataIndex: "oldstuff_id",
                render: (text,record) => (
                    <Space size="small">
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.shenhe(record)}}>审核</Button>
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.del(text)}}
                                disabled={this.props.userInfo.uinfo.jurisdiction.issh !== '1' && this.props.userInfo.uinfo.username !== 'admin'}
                        >删除</Button>
                    </Space>
                )
            }
        ];
        const jobColumns = [
            {
                title: "创建日期",
                dataIndex: "createtime",
                render:text => <span>{this.dateFormat(text)}</span>
            },
            {
                title: "作者",
                dataIndex: "nickname",
            },
            {
                title: "岗位名称",
                dataIndex: "job_name",
            },
            {
                title: "薪资",
                dataIndex: "job_salary",
            },
            {
                title: "分类",
                dataIndex: "job_lable",
            },
            {
                title: "状态",
                dataIndex: "ispublic",
                render: text => {
                    if(text === 1) return <span style={{color: "rgb(108, 187, 122)"}}>审核通过</span>
                    else if (text === 0) return <span style={{color: "#409EFF"}}>未审核</span>
                    else return <span style={{color: "rgb(246, 12, 108)"}}>审核未通过</span>
                }
            },
            {
                title: "审核人",
                dataIndex: "admin",
            },
            {
                title: "操作",
                dataIndex: "job_id",
                render: (text,record) => (
                    <Space size="small">
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.shenhe(record)}}>审核</Button>
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.del(text)}}
                                disabled={this.props.userInfo.uinfo.jurisdiction.issh !== '1' && this.props.userInfo.uinfo.username !== 'admin'}
                        >删除</Button>
                    </Space>
                )
            }
        ];
        const articleColumns = [
            {
                title: "创建日期",
                dataIndex: "createtime",
                render:text => <span>{this.dateFormat(text)}</span>
            },
            {
                title: "作者",
                dataIndex: "nickname",
            },
            {
                title: "标题",
                dataIndex: "article_title",
            },
            {
                title: "分类",
                dataIndex: "article_lable",
            },
            {
                title: "状态",
                dataIndex: "ispublic",
                render: text => {
                    if(text === 1) return <span style={{color: "rgb(108, 187, 122)"}}>审核通过</span>
                    else if (text === 0) return <span style={{color: "#409EFF"}}>未审核</span>
                    else return <span style={{color: "rgb(246, 12, 108)"}}>审核未通过</span>
                }
            },
            {
                title: "审核人",
                dataIndex: "admin",
            },
            {
                title: "操作",
                dataIndex: "article_id",
                render: (text,record) => (
                    <Space size="small">
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.shenhe(record)}}>审核</Button>
                        <Button type="text" style={{color:"#409EFF"}} size={"small"} onClick={()=>{this.del(text)}}
                                disabled={this.props.userInfo.uinfo.jurisdiction.issh !== '1' && this.props.userInfo.uinfo.username !== 'admin'}
                        >删除</Button>
                    </Space>
                )
            }
        ];
        return (
            <div>
                <div className="top">
                    <Breadcrumb>
                        <Breadcrumb.Item>
                            <NavLink to='/'>首页</NavLink>
                        </Breadcrumb.Item>
                        <Breadcrumb.Item>
                            审核中心
                        </Breadcrumb.Item>
                        <Breadcrumb.Item>
                            内容审核
                        </Breadcrumb.Item>
                    </Breadcrumb>
                </div>
                <div className="main">
                    <div>
                        <Form onFinish={this.getContentList} layout={"inline"}>
                            <Form.Item name="user" initialValue="" style={{width: '15%', margin:"15px 10px 22px 0"}}>
                                <Input placeholder="用户"></Input>
                            </Form.Item>
                            <Form.Item name="admin" initialValue="" style={{width: '15%', margin:"15px 10px 22px 0"}}>
                                <Input placeholder="审批人"></Input>
                            </Form.Item>
                            <Form.Item name="search" initialValue="" style={{width: '15%', margin:"15px 10px 22px 0"}}>
                                <Input placeholder="关键字"></Input>
                            </Form.Item>
                            <Form.Item name="state" initialValue="0" style={{width: '15%', margin:"15px 10px 22px 0"}}>
                                <Select placeholder="状态">
                                    <Option value="">全部</Option>
                                    <Option value="0">待审核</Option>
                                    <Option value="1">审核通过</Option>
                                    <Option value="-1">审核未通过</Option>
                                </Select>
                            </Form.Item>
                            <Form.Item name="type" initialValue="help" style={{width: '15%', margin:"15px 10px 22px 0"}}>
                                <Select placeholder="模块">
                                    <Option value="help">问答交流</Option>
                                    <Option value="activity">校园活动</Option>
                                    <Option value="oldstuff">二手信息</Option>
                                    <Option value="job">招聘信息</Option>
                                    <Option value="article">文章新闻</Option>
                                </Select>
                            </Form.Item>
                            <Form.Item style={{margin:"15px 10px 22px 0"}}>
                                <Button type="primary" htmlType="submit">查询</Button>
                            </Form.Item>
                        </Form>
                    </div>
                    {/*弹窗*/}
                    <Modal visible={dialog} title="详情"
                           onCancel={()=>this.setState({dialog:false})}
                           footer={[
                               <Button key="pass" type="primary" onClick={()=>this.changeState(1)}>
                                   通过审核
                               </Button>,
                               <Button key="fail" type="danger" onClick={()=>this.changeState(-1)}>
                                   不通过审核
                               </Button>,
                               <Button key="back" onClick={()=>this.setState({dialog:false})}>
                                   取消
                               </Button>,
                           ]}
                    >
                        {
                            pagelistquery.type==='help'?
                                <Form>
                                    <Form.Item label="标题">{content.help_title}</Form.Item>
                                    <Form.Item label="分类">{content.help_lable}</Form.Item>
                                    <Form.Item label="内容">
                                        <span dangerouslySetInnerHTML={{__html: content.help_content}}></span>
                                    </Form.Item>
                                </Form>:null
                        }
                        {
                            pagelistquery.type==='activity'?
                                <Form>
                                    <Form.Item label="标题">{content.activity_title}</Form.Item>
                                    <Form.Item label="分类">{content.activity_lable}</Form.Item>
                                    <Form.Item label="地点">{content.activity_locale}</Form.Item>
                                    <Form.Item label="时间">
                                        {this.dateFormat(content.activity_statetime)}--{this.dateFormat(content.activity_endtime)}
                                    </Form.Item>
                                    <Form.Item label="内容">
                                        <span dangerouslySetInnerHTML={{__html: content.activity_content}}></span>
                                    </Form.Item>
                                </Form>:null
                        }
                        {
                            pagelistquery.type==='job'?
                                <Form>
                                    <Form.Item label="岗位">{content.job_name}</Form.Item>
                                    <Form.Item label="薪资">{content.job_salary}</Form.Item>
                                    <Form.Item label="人数">{content.job_num}</Form.Item>
                                    <Form.Item label="分类">{content.job_lable}</Form.Item>
                                    <Form.Item label="内容">
                                        <span dangerouslySetInnerHTML={{__html: content.job_content}}></span>
                                    </Form.Item>
                                </Form>:null
                        }
                        {
                            pagelistquery.type==='article'?
                                <Form>
                                    <Form.Item label="标题">{content.article_title}</Form.Item>
                                    <Form.Item label="分类">{content.article_lable}</Form.Item>
                                    <Form.Item label="介绍">{content.article_introduction}</Form.Item>
                                    <Form.Item label="内容">
                                        <span dangerouslySetInnerHTML={{__html: content.article_content}}></span>
                                    </Form.Item>
                                </Form>:null
                        }
                        {
                            pagelistquery.type==='oldstuff'?
                                <Form>
                                    <Form.Item label="实物">
                                        <img src={content.oldstuff_img} alt="图片地址错误" width="300px"/>
                                    </Form.Item>
                                    <Form.Item label="名称">{content.oldstuff_name}</Form.Item>
                                    <Form.Item label="价格">{content.oldstuff_price}</Form.Item>
                                    <Form.Item label="分类">{content.oldstuff_lable}</Form.Item>
                                    <Form.Item label="描述">
                                        <span dangerouslySetInnerHTML={{__html: content.oldstuff_content}}></span>
                                    </Form.Item>
                                </Form>:null
                        }
                    </Modal>
                    {/*表格*/}
                    {
                        type==="help"?
                            <Table dataSource={tableData} columns={helpColumns} bordered
                                   loading={loading}
                                   pagination={{
                                       total:pagelistquery.total,
                                       showSizeChanger: true,
                                       showQuickJumper: true,
                                       onChange: this.handleCurrentChange,
                                       onShowSizeChange: this.handleSizeChange,
                                       showTotal: total => `共 ${total} 条`,
                                       position:["bottomLeft"]
                                   }}
                            ></Table>:null
                    }
                    {
                        type==="activity"?
                            <Table dataSource={tableData} columns={activityColumns} bordered
                                   loading={loading}
                                   pagination={{
                                       total:pagelistquery.total,
                                       showSizeChanger: true,
                                       showQuickJumper: true,
                                       onChange: this.handleCurrentChange,
                                       onShowSizeChange: this.handleSizeChange,
                                       showTotal: total => `共 ${total} 条`,
                                       position:["bottomLeft"]
                                   }}
                            ></Table>:null
                    }
                    {
                        type==="oldstuff"?
                            <Table dataSource={tableData} columns={oldstuffColumns} bordered
                                   loading={loading}
                                   pagination={{
                                       total:pagelistquery.total,
                                       showSizeChanger: true,
                                       showQuickJumper: true,
                                       onChange: this.handleCurrentChange,
                                       onShowSizeChange: this.handleSizeChange,
                                       showTotal: total => `共 ${total} 条`,
                                       position:["bottomLeft"]
                                   }}
                            ></Table>:null
                    }
                    {
                        type==="job"?
                            <Table dataSource={tableData} columns={jobColumns} bordered
                                   loading={loading}
                                   pagination={{
                                       total:pagelistquery.total,
                                       showSizeChanger: true,
                                       showQuickJumper: true,
                                       onChange: this.handleCurrentChange,
                                       onShowSizeChange: this.handleSizeChange,
                                       showTotal: total => `共 ${total} 条`,
                                       position:["bottomLeft"]
                                   }}
                            ></Table>:null
                    }
                    {
                        type==="article"?
                            <Table dataSource={tableData} columns={articleColumns} bordered
                                   loading={loading}
                                   pagination={{
                                       total:pagelistquery.total,
                                       showSizeChanger: true,
                                       showQuickJumper: true,
                                       onChange: this.handleCurrentChange,
                                       onShowSizeChange: this.handleSizeChange,
                                       showTotal: total => `共 ${total} 条`,
                                       position:["bottomLeft"]
                                   }}
                            ></Table>:null
                    }
                </div>
            </div>
        );
    }
}

export default Content;